<?php

/**
 * This class defines a user principal resource.
 *
 * Copyright © 2010 Jack Cleaver
 * 
 * This plugin provides functionality added by CalDAV (RFC 4791)
 * It implements new reports, and the MKCALENDAR method.
 *
 * This file is part of Slash.
 *
 * Slash is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Slash is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Slash.  If not, see <http://www.gnu.org/licenses/>.
 */
class Slash_ACL_FSExt_Principal
extends Slash_ACL_FSExt_Directory
{

	private $isGroup;
	
	public function __construct($path)
	{
		parent::__construct($path);
		//Slash_Debug::debug("Construct principal for path $this->path");
		$this->propertyMap[] = "{DAV:}displayname";
		$this->propertyMap[] = "{DAV:}owner";
		//Slash_Debug::debugDump("in Principal constructor: ", $this->propertyMap);
	}
	/**
	 * Updates the name of this object
	 *
	 * @param string $name
	 * @return void
	 */
	public function setName($name)
	{
		throw new Sabre_DAV_Exception_Forbidden();
	}

	public function isGroup()
	{
		if (!isset($this->isGroup))
		{
			$propName = "{DAV:}group-member-set";
			$propreq = array($propName);
			$props = $this->getProperties($propreq);
			$this->isGroup = (isset($props[$propName]));
		}
		return $this->isGroup;
	}
	
	/**
	 * Creates a new file under this object.
	 *
	 * This is currently not allowed
	 *
	 * @param string $filename
	 * @param resource $data
	 * @return void
	 */
	public function createFile($filename, $data=null)
	{
		throw new Sabre_DAV_Exception_MethodNotAllowed('Creating new files in this collection is not supported');
	}


	public function updatePrincipal(array $properties)
	{

		$newValues = array();
		$result = array(
		200 => array(), // Ok
		403 => array(), // Forbidden
		424 => array(), // Failed Dependency
		);

		$hasError = false;

		//Slash_Debug::debugDump("ACL_FSExt_Principal updatePrincipal, properties: ", $properties);
			

		if ($this->updateProperties($properties))
		{
			Slash_Debug::debug("ACL_FSExt_Principal updatePrincipal, updated properties OK");
			$result[200] = $properties;
		}
		return $result;
		//return true;
		// Success

	}
	

	
	public function getChild($name)
	{
		$path = $this->path . '/' . $name;
		//Slash_Debug::debug("ACL_FSExt_Principal getChild: $name, $path");
			

		if (is_dir($path))
		{
			$node = new Sabre_DAV_FSExt_Directory($path);
			return $node;
		} else if (is_file($path))
		{
			return new Sabre_DAV_FSExt_File($path);
		} else {
			throw new Sabre_DAV_Exception_FileNotFound('Principal getChild: The resource you tried to access does not exist');
		}
	}
}
